<?php
/**
 * Created by PhpStorm.
 * User: Максим
 * Date: 22.07.13
 * Time: 15:46
 */
ini_set("memory_limit", "1024M");

function get_url($link, $model)
{
    $model = urlencode($model);
    $link = str_ireplace('exist.ua', 'www.elcats.ru', $link);
    $link = str_ireplace('Parts.axd', 'fullinfo.aspx', $link);
    $url = $link . "&marka=$model&office=ru";
    return $url;
}

function insert_model($model, $years, $url, $oe, $marka)
{
    $marka_id = null;
    $oe_id = null;

    global $db;

    $result = $db->query("SELECT `id` FROM `ex_marka` WHERE marka = '$marka'");
    if ($result->num_rows > 0)
    {
        $marka_id = $result->fetch_object()->id;
    }
    else
    {
        $db->query("INSERT INTO `ex_marka` (`marka`) VALUES ('$marka')");
        $marka_id  = $db->insert_id;
    }
    $result = null;

    $result = $db->query("SELECT `id` FROM `ex_oe_codes` WHERE oe = '$oe' AND marka_id = '$marka_id'");
    if ($result->num_rows > 0)
    {
        $oe_id = $result->fetch_object()->id;
    }
    else
    {
        $db->query("INSERT INTO `ex_oe_codes` (`oe`, `marka_id`) VALUES ('$oe', '$marka_id')");
        $oe_id = $db->insert_id;
    }
    $result = null;

    $result = $db->query("
        SELECT `id` FROM `ex_model`
        WHERE `model` = '$model'
        AND `link` = '$url' AND `marka_id` = '$marka_id'
        AND `years` = '$years' AND `oe_id` = '$oe_id'");

    if ($result->num_rows == 0)
    {
        $db->query("
            INSERT INTO `ex_model`
                (`model`, `link`, `marka_id`, `years`, `oe_id`)
            VALUES
                ('$model', '$url', '$marka_id', '$years', '$oe_id')");
        print "New - " . $model . " - " . $years . " - " . $url . " - " . $oe . " - " . $marka ."\n";
    }
    else
    {
        print "Exist - " . $model . " - " . $years . " - " . $url . " - " . $oe . " - " . $marka ."\n";
    }
}


include 'simple_html_dom.php';


$db = new mysqli('avtopro.msgroup.ua', 'u_avtopro2', 'H1AuXxH6', 'avtopro');


$result = $db->query("SELECT * FROM `ex_app_links` WHERE (bad IS NULL OR bad <> 1) AND `manufacture` = 'Chevrolet'");


while ($row = $result->fetch_object()):


    $html = new simple_html_dom($row->html);


    $models_string = $html->find('#lblModels', 0)->innertext;
    $tmp_models = str_ireplace('&nbsp;', '', explode("|", $models_string));

    $models = array();

    foreach ($tmp_models as $tmp_model)
    {
        $model = strip_tags($tmp_model);
        $models[] = trim($model);
    }
    $html = null; $tmp_models = null;

    foreach ($models as $model):
        $url = get_url($row->link, $model);

        $html = file_get_html($url);

        $tblTable = $html->find('#tblTable tr');

        if (is_null($tblTable))
            continue;
        else
        {
            array_shift($tblTable);
            foreach ($tblTable as $tr)
            {
                $td_model = strip_tags($tr->children(0));
                $td_years = strip_tags($tr->children(2));

                insert_model($td_model, $td_years, $url, $row->oe, $row->manufacture);
            }
        }
    endforeach;
endwhile;




